# Transform operators

## map

Applies a converter function to converts the observed value into a new value.

```rx
-1--2--3-

> map(x => x * x)

-1--4--9-
```

```ts
import { from, map } from "@devicescript/observables"

const obs = from([1, 2, 3])

obs
    // highlight-next-line
    .pipe(map(v => v * v))
    .subscribe(v => console.log(v))
```

## scan

Applies an accumulator (or "reducer function") to each value from the source.

```rx
-1---2---3-

> scan((p, c) => p + c, 0)

-1---3---6-
```

```ts
import { from, scan } from "@devicescript/observables"

const obs = from([1, 2, 3])

obs.pipe(
    // highlight-next-line
    scan((p, c) => p + c, 0)
).subscribe(v => console.log(v))
```

## timestamp

Wraps value in an object with a `timestamp` property and `lastTimestamp` property.

```rx
[styles]
event_radius = 60
operator_height = 60

-a--

> timestamp()

-a--
a := { value: a, ... }
```

```ts
import { from, timestamp } from "@devicescript/observables"

const obs = from([1, 2, 3])

obs.pipe(
    // highlight-next-line
    timestamp()
).subscribe(v => console.log(v))
```

## switchMap

Takes a source Observable and calls transform(T) for each emitted value.
It will immediately subscribe to any Observable coming from transform(T), b
ut in addition to this, will unsubscribe() from any prior Observables -
so that there is only ever one Observable subscribed at any one time.

```rx
--1-----2--3--4--------

> switchMap(v => from([v+1, v+1, v+1]))

--2--2--3--4--5--5--5--
```

```ts
import { from, switchMap } from "@devicescript/observables"

const obs = from([1, 2, 3])

obs.pipe(
    // highlight-next-line
    switchMap(v => from([v + 1, v + 1, v + 1]))
).subscribe(v => console.log(v))
```
